package com.tanhua.manage.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.common.enums.LogoStatusEnum;
import com.tanhua.common.enums.TagsEnum;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.manage.mapper.UserInfosMapper;
import com.tanhua.manage.pojo.Log;
import com.tanhua.manage.pojo.UserStatus;
import com.tanhua.dubbo.server.api.UserLikeApi;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.manage.vo.PageInfo;
import com.tanhua.manage.vo.UserVo;
import com.tanhua.manage.vo.UsersManageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class UsersManageService {

    @Autowired
    private UserInfosMapper userInfosMapper;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private LogService logService;

    @Autowired
    private UserStatusService userStatusService;

    @Reference(version = "1.0.0")
    private UserLikeApi userLikeApi;


    /**
     * 将用户数据进行翻页
     *
     * @param page
     * @param pagesize
     * @return
     */
    public PageInfo<UserVo> queryByPage(Long page, Long pagesize) {

        //设置页数
        PageInfo<UserVo> pageInfo = new PageInfo<>();
        pageInfo.setPage(page);
        pageInfo.setPageSize(pagesize);

        //查询总条数
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        Integer count = userInfosMapper.selectCount(queryWrapper);
        pageInfo.setCounts(count);

        //查询总页数
        pageInfo.setPages(count / pagesize + 1);


        // 填充UserInfoVo信息
        List<UserVo> userVoList = new ArrayList<>();
        IPage<UserInfo> iPage = userInfoService.queryUserInfoByPage(page, pagesize);
        List<UserInfo> userInfoList = iPage.getRecords();

        for (UserInfo userInfo : userInfoList) {
            UserVo userVo = new UserVo();
            userVo.setId(Integer.valueOf(userInfo.getUserId().toString()));
            userVo.setLogo(userInfo.getLogo());
            userVo.setLogoStatus(LogoStatusEnum.yes);
            userVo.setNickname(userInfo.getNickName());
            User user = usersService.QueryUserMobileByUserID(userInfo.getUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                userVo.setMobile(user.getMobile());
            }
            userVo.setSex(userInfo.getSex());
            userVo.setAge(userInfo.getAge());
            userVo.setOccupation(userInfo.getIndustry());
            UserStatus userStatus = userStatusService.QueryUserStatusById(userInfo.getUserId());
            if (ObjectUtil.isNotEmpty(userStatus)) {
                userVo.setUserStatus(String.valueOf(userStatus.getStatus()));
            }
            userVo.setLastActiveTime(45614564L);
            //设置注册地区
            if (userInfo.getCity().contains("-")) {
                String[] split = userInfo.getCity().split("-");
                userVo.setCity(split[split.length - 1]);
            } else {
                userVo.setCity(userInfo.getCity());
            }
            userVoList.add(userVo);
        }
        pageInfo.setItems(userVoList);
        return pageInfo;
    }


    /**
     * 根据id查询用户所有数据
     *
     * @param userId 用户id
     * @return
     */
    public UsersManageVo queryUsersById(Long userId) {

        //查询userinfo数据
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(userId);
        UsersManageVo usersManageVo = new UsersManageVo();

        usersManageVo.setId(Math.toIntExact(userId));//用户id
        usersManageVo.setNickname(userInfo.getNickName());//用户昵称
        //查询用户手机号
        User user = usersService.QueryUserMobileByUserID(userId);
        usersManageVo.setMobile(user.getMobile());

        usersManageVo.setSex(userInfo.getSex());//用户性别
        usersManageVo.setPersonalSignature("这y1刻,涐ゝ终亍明苩′什麽昰责任、什麽昰吢痛");//个性签名
        usersManageVo.setAge(userInfo.getAge());//年龄

        Long likeCount = userLikeApi.queryLikeCounts(userId);//喜欢数
        usersManageVo.setCountBeLiked(likeCount.intValue());

        Long fanCount = userLikeApi.queryFanCount(userId);
        usersManageVo.setCountLiked(fanCount.intValue());//粉丝

        Long likeCounts = userLikeApi.queryMutualLikeCount(userId);
        usersManageVo.setCountMatching(likeCounts.intValue());// TODO: 2021/8/4

        usersManageVo.setIncome(Integer.valueOf(userInfo.getIncome()));//收入
        usersManageVo.setOccupation(userInfo.getIndustry());//职业

        UserStatus userStatus = userStatusService.QueryUserStatusById(userId);
        usersManageVo.setUserStatus(userStatus.getStatus());//用户状态
        //糊涂工具包将date日期格式转换为integer格式
        Integer createTime = Convert.toInt(userInfo.getCreated());
        usersManageVo.setCreated(createTime);//注册时间

        //设置注册地区
        if (userInfo.getCity().contains("-")) {
            String[] split = userInfo.getCity().split("-");
            usersManageVo.setCity(split[split.length - 1]);
        } else {
            usersManageVo.setCity(userInfo.getCity());
        }
        //查询Log用户信息
        Log log = logService.QueryLogById(userId);

        String logTime = log.getLogTime();//2020-11-11
        Calendar instance = Calendar.getInstance();
        try {
            instance.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(logTime));
//            aUsersManageVo.setLastActiveTime(instance.getTimeInMillis());//最近活跃时间
            usersManageVo.setLastActiveTime(1186514454);//最近活跃时间
        } catch (ParseException e) {
            e.printStackTrace();
        }
        usersManageVo.setLastLoginLocation(log.getPlace());//登陆地

        usersManageVo.setLogo(userInfo.getLogo());//头像
        usersManageVo.setTags(TagsEnum.music);//用户标签

        return usersManageVo;

    }

}
